package org.example.common.social.utils;

import cn.hutool.core.util.ObjectUtil;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.*;
import org.example.common.core.utils.SpringUtils;
import org.example.common.social.config.properties.SocialLoginConfigProperties;
import org.example.common.social.config.properties.SocialProperties;
import org.example.common.social.maxkey.AuthMaxKeyRequest;
import org.example.common.social.maxkey.AuthWechatAppletRequest;

/**
 * 认证授权工具类
 *
 * @author thiszhc
 */
public class SocialUtils  {

    private static final AuthRedisStateCache STATE_CACHE = SpringUtils.getBean(AuthRedisStateCache.class);

    @SuppressWarnings("unchecked")
    public static AuthResponse<AuthUser> loginAuth(String source, String code, String state, SocialProperties socialProperties) throws AuthException {
        AuthRequest authRequest = getAuthRequest(source, socialProperties);
        AuthCallback callback = new AuthCallback();
        callback.setCode(code);
        callback.setState(state);
        return authRequest.login(callback);
    }

    public static AuthRequest getAuthRequest(String source, SocialProperties socialProperties) throws AuthException {
        SocialLoginConfigProperties obj = socialProperties.getType().get(source);
         if (ObjectUtil.isNull(obj)) {
            throw new AuthException("不支持的第三方登录类型");
        }
        final AuthConfig.AuthConfigBuilder builder = AuthConfig.builder()
            .clientId(obj.getClientId())
            .clientSecret(obj.getClientSecret())
            .redirectUri(obj.getRedirectUri());
        AuthRequest request=null;
        switch (source.toLowerCase()) {
            case "dingtalk": request=new AuthDingTalkRequest(builder.build(), STATE_CACHE); break;
            case "baidu": request=new AuthBaiduRequest(builder.build(), STATE_CACHE);break;
            case "github" : request=new AuthGithubRequest(builder.build(), STATE_CACHE);break;
            case "gitee" : request=new AuthGiteeRequest(builder.build(), STATE_CACHE);break;
            case "weibo" : request=new AuthWeiboRequest(builder.build(), STATE_CACHE);break;
            case "coding": request=new AuthCodingRequest(builder.build(), STATE_CACHE);break;
            case "oschina" : request=new AuthOschinaRequest(builder.build(), STATE_CACHE);break;
            // 支付宝在创建回调地址时，不允许使用localhost或者127.0.0.1，所以这儿的回调地址使用的局域网内的ip
            case "alipay_wallet" : request=new AuthAlipayRequest(builder.build(),
                    socialProperties.getType().get("alipay_wallet").getAlipayPublicKey(), STATE_CACHE);break;
            case "qq" : request=new AuthQqRequest(builder.build(), STATE_CACHE);break;
            case "wechat_open": request=new AuthWeChatOpenRequest(builder.build(), STATE_CACHE);break;
            case "taobao" : request=new AuthTaobaoRequest(builder.build(), STATE_CACHE);break;
            case "douyin": request=new AuthDouyinRequest(builder.build(), STATE_CACHE);break;
            case "linkedin": request=new AuthLinkedinRequest(builder.build(), STATE_CACHE);break;
            case "microsoft" : request=new AuthMicrosoftRequest(builder.build(), STATE_CACHE);break;
            case "renren" : request=new AuthRenrenRequest(builder.build(), STATE_CACHE);break;
            case "stack_overflow" : request=new AuthStackOverflowRequest(builder.stackOverflowKey("").build(), STATE_CACHE);break;
            case "huawei": request=new AuthHuaweiRequest(builder.build(), STATE_CACHE);break;
            case "wechat_enterprise": request=new AuthWeChatEnterpriseQrcodeRequest(builder.agentId("").build(), STATE_CACHE);break;
            case "gitlab": request=new AuthGitlabRequest(builder.build(), STATE_CACHE);break;
            case "wechat_mp": request=new AuthWeChatMpRequest(builder.build(), STATE_CACHE);break;
            case "aliyun": request=new AuthAliyunRequest(builder.build(), STATE_CACHE);break;
            case "maxkey": request=new AuthMaxKeyRequest(builder.build(), STATE_CACHE);break;
            case "wechat_applet": request=new AuthWechatAppletRequest(builder.ignoreCheckRedirectUri(true).build());break;
            default: throw new AuthException("未获取到有效的Auth配置");
        }
        return request;
    }
}

